<?php
namespace App\Http\Controllers\Api;


use App\AdvertImg;
use App\Banner;
use App\BannerNav;
use App\BannerTwo;
use App\Express;
use App\Http\Services\DataManager;
use App\Http\Services\FileManager;
use App\IntegralData;
use App\QrCodes;
use App\ShowGoods;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Classes\PHPExcel;
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Symfony\Component\CssSelector\Parser\Reader;


class OtherController extends Controller
{
    /**
     * 获取banner图
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getBannerList (Request $request)
    {
        $type = !is_null($request->get('type')) ? $request->get('type') : "home";


        $allBanner = Banner::whereType($type)->orderBy("sort","desc")->get();
        if (count($allBanner) > 0) {
            foreach ($allBanner as $banner) {
                /** @var Banner $banner */
                $banner->img_url = FileManager::getImgUrl($banner->img_id);
            }
        }

        return response()->json(["level" => "success", "message" => "获取成功", "data" => $allBanner]);
    }

    /**
     * 获取游戏banner
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGameBannerList(Request $request){
        $allBanner = BannerTwo::orderBy("sort","desc")->get();
        if (count($allBanner) > 0) {
            foreach ($allBanner as $banner) {
                /** @var BannerTwo $banner */
                $banner->img_url = FileManager::getImgUrl($banner->img_id);
            }
        }

        return response()->json(["level" => "success", "message" => "获取成功", "data" => $allBanner]);

    }

    public function getBannerNavList (Request $request)
    {

        $allBannerNav = BannerNav::whereStatus(1)->orderBy('created_at', 'desc')->get();
        if (count($allBannerNav) > 0) {
            foreach ($allBannerNav as $banner) {
                /** @var Banner $banner */
                $banner->img_url = FileManager::getImgUrl($banner->img_id);
            }
        }

        return response()->json(["level" => "success", "message" => "获取成功", "data" => $allBannerNav]);
    }


    /**
     * 获取快递公司
     * @return \Illuminate\Http\JsonResponse
     */
    public function getExpressCompany ()
    {
        $express = Express::whereIsDefault(1)->first();

        return response()->json(['level' => 'success', 'message' => '获取成功', 'data' => $express]);
    }

    /**
     * 获取首页广告图
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAdvertImg (Request $request)
    {
        $type = !is_null($request->get('type')) && $request->get('type') != '' ? $request->get('type') : 'common';
        /** @var AdvertImg $advertImg */
        $allAdvertImg = AdvertImg::whereType($type)->orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();
        if (count($allAdvertImg) > 0) {
            foreach ($allAdvertImg as $advertImg) {
                $advertImg->img_url = FileManager::getImgUrl($advertImg->img_id);
            }
        }

        $showGoods = ShowGoods::whereId(1)->first();

        return response()->json(['level' => 'success','message' => '获取成功','data' => $allAdvertImg,'show_goods' => $showGoods ? $showGoods->show_goods_two : null]);
    }

    public function excel(Request $request){
        //> 判断请求类型


        if( $request->isMethod('post') && $_FILES['file'] ){



            ini_set('memory_limit','128M');
            $stime=microtime(true);
            //> 获取上传文件路径 $_FILES
            if( $_FILES['file']['error'] == 0 ){
                //> 获取上传文件名称(已便于后面判断是否上传需要后缀文件)
                $name = $_FILES['file']['name'];
                //> 获取上传文件后缀 如(xls exe xlsx 等)
                $ext = strtolower(trim(substr($name,(strpos($name,'.')+1))));
                //> 判断文件是否为指定的上传文件后缀
                if( ! in_array($ext,array('xls','xlsx')) ){
                    //> 返回上一次请求位置,并携带错误消息
                    return redirect()->back()->withErrors('请输入xls或xlsx后缀文件')->withInput();
                }
                //> 获取文件上传路径
                $fileName = $_FILES['file']['tmp_name'];
                                Excel::load($fileName, function ($reader) {
                                    $i=0;
                                    foreach ($reader->takeRows(50000)->get() as $item){
                                     dump($item->toArray());

                                        if(count($item->toArray())>0){
                                            foreach ( $item->toArray() as $item){

                                                $key=str_replace('http://m.f6shot.com/c/?','', $item['url']);


                                                DB::insert('insert into qrcodes(q_key,url,integral,created_at)value (?,?,?,?)',[$key,$item['url'],4,date('Y-m-d H:i:s')]);

                                                unset($item);
                                                $i++;

                                            }

                                            dump($i);

                                        }
                                    }

                                });
            }

            $etime=microtime(true);//获取程序执行结束的时间
            $total=$etime-$stime;  //计算差值

            dump($total);

            exit;
        }else{
            return response()->json(['level' => 'error','message' => '请上传表格']);

        }
    }

    public  function excelData(Request $request){



        if( $request->isMethod('post') && $_FILES['file'] ){



            ini_set('memory_limit','128M');
            $stime=microtime(true);
            //> 获取上传文件路径 $_FILES
            if( $_FILES['file']['error'] == 0 ){
                //> 获取上传文件名称(已便于后面判断是否上传需要后缀文件)
                $name = $_FILES['file']['name'];
                //> 获取上传文件后缀 如(xls exe xlsx 等)
                $ext = strtolower(trim(substr($name,(strpos($name,'.')+1))));
                //> 判断文件是否为指定的上传文件后缀
                if( ! in_array($ext,array('xls','xlsx')) ){
                    //> 返回上一次请求位置,并携带错误消息
                    return redirect()->back()->withErrors('请输入xls或xlsx后缀文件')->withInput();
                }
                //> 获取文件上传路径
                $fileName = $_FILES['file']['tmp_name'];




                Excel::load($fileName, function ($reader) {


                    foreach ($reader->get() as $item) {
                        $allNum = count($item->toArray());


                        $allNum = $allNum - $allNum % 1000;
                        $a = $allNum / 1000;

                          /*  $a=$a-1;*/

                        for($b=0;$b<$a;$b++){
                            $data=[];

                            foreach ($reader->skipRows(1000*$b)->takeRows(1000)->get() as $item){

                                if (count($item->toArray()) > 0) {
                                    foreach ($item->toArray() as $aa) {


                                        $key=str_replace('http://m.f6shot.com/c/?','', $aa['url']);
                                        $url=$aa['url'];
                                        $da=array('q_key'=>$key,'url'=>$url,'integral'=>8,'created_at'=>date('Y-m-d H:i:s'));
                                        array_push($data,$da);
                                    }
                                    DB::table('qrcodes')->insert($data);
                                }

                            }

                        }
                    }
                });
            }

            $etime=microtime(true);//获取程序执行结束的时间
            $total=$etime-$stime;  //计算差值

            dump($total);

            exit;
        }else{
            return response()->json(['level' => 'error','message' => '请上传表格']);

        }



    }



}
